package com.examplemartin.jpademo.dao;

import com.examplemartin.jpademo.pojo.po.good.GoodPO;
import com.examplemartin.jpademo.pojo.po.stock.RepoInfoPO;
import com.examplemartin.jpademo.pojo.po.stock.StockPO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface RepoInfoDao extends JpaRepository<RepoInfoPO,Long> {

    // 查询 -- 根据商品和仓库
    public List<RepoInfoPO> findAllByStockPOAndGoodPO(StockPO stockPO, GoodPO goodPO);

    /**
     * 库存内所有商品 ,不同库存合并处理,出数量
     * @return
     */
    @Query("select t1.goodPO.id  ,sum(t1.goodcount) ,sum(t1.goodspare) from t_repoinfo t1 group by t1.goodPO")
    public List<Object[]> findAllGroupByGoodId();

    @Query("from t_repoinfo t1 where t1.goodcount <= t1.threshold")
    List<RepoInfoPO> findByShortage();

    List<RepoInfoPO> findAllByGoodPO(GoodPO goodPO);

    // 查询需要补货
    @Query("from t_repoinfo t1 where t1.goodcount < t1.threshold")
    List<RepoInfoPO> findAllNeedReplenish();

  List<RepoInfoPO> findAllByStockPO(StockPO byId);
}
